$(function() {
    // 设置一个变量用于储存验证码
    var vcode;




    // 给确认密码添加一个鼠标离开事件
    $('.sureNewPass').on('blur', function() {
        var newPass = $('.newPass').val();
        var sureNewPass = $('.sureNewPass').val();
        if (newPass != sureNewPass) {
            // alert(111);
            mui.alert('两次输入密码不一致，请重新输入', '提示', '确定', mui.closePopups(), 'div');
            return;
        }
    });






    $('.getCode').on('tap', function() {

        var originPass = $('.originPass').val();
        var newPass = $('.newPass').val();
        var sureNewPass = $('.sureNewPass').val();
        var reg = /^[0-9]{8,11}$/;
        if (!reg.test(newPass)) {
            mui.alert('新密码不正确，请重新输入', '提示', '确定', mui.closePopups(), 'div');
            return;
        } else if (!reg.test(sureNewPass)) {
            mui.alert('请确认密码', '提示', '确定', mui.closePopups(), 'div');
            return;
        }

        if ($('.getCode').val()) {
            $('.getCodes').attr('disabled', false);
        }
        // console.log(11);


        $.ajax({
            type: 'get',
            url: "/user/vCodeForUpdatePassword",
            beforeSend: function() {
                $('.getCode').addClass('getCodes');

                var time = 60;

                var timer = setInterval(function() {

                    time--;


                    $('.getCode').text(time + '秒后重试');

                    if (time == 0) {

                        $('.getCodes').text('获取验证码').attr('disabled', true).removeClass('getCodes');

                        clearInterval(timer);
                    }
                }, 1000)
            },
            // 1.2.接收验证码，给出相应的提示
            success: function(res) {
                // console.log(res);
                var str = res.vCode;
                // alert(str);
                mui.alert(str, '提示!获取成功', '确定', mui.closePopups(), 'div');
                vcode = str;
            }
        });



    });



    // 给修改按钮注册点击事件
    $('#modifyBtn').on('click', function() {

        var originPass = $('.originPass').val();
        var newPass = $('.newPass').val();

        var checkCode = $('.checkCode').val();
        if (checkCode != vcode) {
            mui.alert('请确认验证码', '提示', '确定', mui.closePopups(), 'div');
            return;
        }

        $.ajax({
            type: 'POST',
            url: "/user/updatePassword",
            data: {
                oldPassword: originPass,
                newPassword: newPass,
                vCode: vcode
            },
            success: function(e) {
                console.log(e);

                if (e.error == 400) {
                    mui.alert('修改成功', '提示', '确定', mui.closePopups(), 'div');
                    setInterval(function() {
                        window.location.href = "user.html";
                    }, 2000)
                }
            },

            error: function() {
                if (e.error == 401 || e.error == 403) {
                    mui.alert('修改失败', '提示', '确定', mui.closePopups(), 'div');
                }
            }

        });

    });

});